/***************************************************************************
 *   Copyright (C) 2007 by shinda   *
 *   shinda@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "shabadview.h"

#include <QSplitter>
#include <QGroupBox>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QSqlQuery>
#include <QString>
#include <QSqlError>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QRadioButton>
#include <QGroupBox>
#include <QButtonGroup>

ShabadView::ShabadView(QWidget *)
{
	createGui();
}

ShabadView::ShabadView(int id, QWidget *){
	createGui();
	
}

void ShabadView::createGui(){
	qDebug() << "Search Gui Started";	
	
	//Initialize Variables
	splDivider = new QSplitter(Qt::Vertical);
	txtView = new QTextEdit;
	grpDisplayOptions = new QGroupBox;
	fntGurmukhi = new QFont(tr("GurbaniWebThick"),14);
	fntEnglish = new QFont(tr("Arial"),12);
	//Set Variables
	txtView->setFont(*fntGurmukhi);
	txtView->setAlignment (Qt::AlignHCenter );
	
	createOptions();
	
	grpDisplayOptions->setLayout( hblOptions );
	//Add Widgets
	splDivider->addWidget( txtView );
	splDivider->addWidget( grpDisplayOptions );
	setConnections();
	
}
void ShabadView::createOptions(){
	hblOptions = new QHBoxLayout();
	
	vblGroup1 = new QVBoxLayout();

	grpView = new QGroupBox( tr("View") );
	hblView = new QHBoxLayout();
	rgView = new QButtonGroup();
	radTukh = new QRadioButton( tr("Tukh") );
	radShabad = new QRadioButton( tr("Shabad") );
	radPage = new QRadioButton( tr("Page") );

	rgView->addButton( radTukh );
	rgView->addButton( radShabad );
	rgView->addButton( radPage );
	hblView->addWidget( radTukh );
	hblView->addWidget( radShabad );
	hblView->addWidget( radPage );
	
	grpView->setLayout(hblView);
	vblGroup1->addWidget(grpView);

	grpFormat = new QGroupBox( tr("Format") );
	hblFormat = new QHBoxLayout();
	rgFormat = new QButtonGroup();
	radClassic = new QRadioButton( tr("Classic") );
	radParagraph = new QRadioButton( tr("Paragraphs") );
	radContinous = new QRadioButton( tr("Continous") );

	rgFormat->addButton( radClassic );
	rgFormat->addButton( radParagraph );
	rgFormat->addButton( radContinous );
	hblFormat->addWidget( radClassic );
	hblFormat->addWidget( radParagraph );
	hblFormat->addWidget( radContinous );
	grpFormat->setLayout( hblFormat );

	vblGroup1->addWidget( grpFormat );

	vblGroup2 = new QVBoxLayout();

	grpDisplay = new QGroupBox( tr( "Display" ) );
	hblDisplay = new QHBoxLayout();
	rgDisplay = new QButtonGroup();
	radPaad = new QRadioButton( tr("Paad Ched" ) ) ;
	radLardivaar = new QRadioButton( tr("Larivaar") );

	rgDisplay->addButton( radPaad );
	rgDisplay->addButton( radLardivaar );
	hblDisplay->addWidget(radPaad);
	hblDisplay->addWidget(radLardivaar);
	grpDisplay->setLayout( hblDisplay );

	vblGroup2->addWidget( grpDisplay );

	hblOptions->addLayout( vblGroup1 );
	hblOptions->addLayout( vblGroup2 );

}
void ShabadView::loadShabad(int value){
	txtView->clear();
	this->ShabadID = value;

	//TODO Add option recognition - view page, laridaar etc.

	QSqlQuery query( QSqlDatabase::database() );
	QString tempStr, tempStr2, gurmukhi, arth ;
	int currentParagraph, oldParagraph, currentShabad, oldShabad;
	oldParagraph = -1;

	qDebug() << getSQL( value );
	query.exec(getSQL( value )) ;
	qDebug() << "Last Error: "<< query.lastError().text();
	
	while (query.next()) {
		currentParagraph = query.value(4).toInt();
		gurmukhi =  query.value(2).toString();
		arth = query.value(13).toString();

		if ( radLardivaar->isChecked() )
			gurmukhi.remove(QChar(' '), Qt::CaseInsensitive);

		if ( radClassic->isChecked() ){
			txtView->setAlignment (Qt::AlignHCenter );				

			txtView->setCurrentFont(*fntGurmukhi);
			txtView->append( gurmukhi );

			txtView->setCurrentFont(*fntEnglish);
			txtView->append( arth );
			
		}
		else if ( radParagraph->isChecked() ){
			if ( (currentParagraph != oldParagraph)  ){
				//if (oldPargraph = -1) oldParagraph = currentParagraph;
				oldParagraph = currentParagraph;
				txtView->setAlignment (Qt::AlignLeft );

				txtView->setCurrentFont(*fntGurmukhi);
				txtView->append( tempStr  );

				txtView->setCurrentFont(*fntEnglish);
				txtView->append( tempStr2  );			

				tempStr.clear();	
				tempStr2.clear();

				tempStr.append( "\t" + gurmukhi );
				tempStr2.append( "\t" + arth );
			}
			else{
				tempStr.append( " " + gurmukhi );
				tempStr2.append( " " + arth );
			}
		}
		else if (radContinous->isChecked() ){
			tempStr.append( gurmukhi + " " );
		}
	}

	//If theres anything left in the temp str, lets output it now
	if ( !tempStr.isEmpty() ) {
		txtView->setAlignment (Qt::AlignJustify );

		txtView->setCurrentFont(*fntGurmukhi);
		txtView->append( tempStr );

		txtView->setCurrentFont(*fntEnglish);
		txtView->append( tempStr2 );

		tempStr.clear();
		tempStr2.clear();
	}

}

QString ShabadView::getSQL(int value){
	//Basic Script to return standard shabad

	QString temp, join;
	join = "LEFT JOIN arthIndex ON (Shabad.id = arthIndex.ShabadID) LEFT JOIN arth ON (arthIndex.artID = arth.id)";

	if ( radTukh->isChecked() )
		temp.sprintf("SELECT Shabad.*, arth.* FROM Shabad LEFT JOIN arthIndex ON (Shabad.id = arthIndex.ShabadID) LEFT JOIN arth ON (arthIndex.artID = arth.id) WHERE Shabad.id = %d;", value);
	else if ( radShabad->isChecked() )
		temp.sprintf("SELECT Shabad.*, arth.* FROM Shabad LEFT JOIN arthIndex ON (Shabad.id = arthIndex.ShabadID) LEFT JOIN arth ON (arthIndex.artID = arth.id) WHERE Shabad.ShabadID = (SELECT ShabadID FROM Shabad WHERE Shabad.id = %d) ORDER BY Line_No;", value);
	else if ( radPage->isChecked() )
		temp.sprintf("SELECT Shabad.*, arth.* FROM Shabad LEFT JOIN arthIndex ON (Shabad.id = arthIndex.ShabadID) LEFT JOIN arth ON (arthIndex.artID = arth.id) WHERE Shabad.PageNo = (SELECT PageNo FROM Shabad WHERE Shabad.id = %d) ORDER BY Shabad.Page_No, Shabad.ShabadID, Line_No;", value);
	
//Select For English Arth - "SELECT Shabad.gurmukhi, arth From Shabad, arthIndex, arth  where (Shabad.ShabadID = ( SELECT Shabad.ShabadID from Shabad Where id = 8572)) AND (arthIndex.shabadID = Shabad.id) AND (arthIndex.artID = arth.id) ORDER BY Line_No;"
	qDebug() << "SQL TO LOAD: " + temp;
	return temp;
	
}

void ShabadView::updateDisplay(){
	qDebug() << "this";
	loadShabad( this->ShabadID );
}

void ShabadView::setConnections(){
	connect (radTukh,   SIGNAL ( clicked() ), this, SLOT ( updateDisplay() ) );
	connect (radShabad, SIGNAL ( clicked() ), this, SLOT ( updateDisplay() ) );
	connect (radPage,   SIGNAL ( clicked() ), this, SLOT ( updateDisplay() ) );
	connect (radClassic,   SIGNAL ( clicked() ), this, SLOT ( updateDisplay() ) );
	connect (radParagraph,   SIGNAL ( clicked() ), this, SLOT ( updateDisplay() ) );
	connect (radContinous, SIGNAL (clicked() ), this, SLOT ( updateDisplay() ) );
	connect (radLardivaar, SIGNAL (clicked() ), this, SLOT ( updateDisplay() ) );
}

ShabadView::~ShabadView()
{
}


